package com.zhongwang.business.module.main.service;

import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.main.dao.MainDao;
import com.zhongwang.business.module.main.model.DsnProStruct;
import com.zhongwang.business.module.main.model.Main;
import com.zhongwang.business.module.main.model.productMixModel;
import com.zhongwang.business.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.ServerEndpoint;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MainService {
    @Autowired
    private MainDao mainDao;
    /**
     * 查询项目查询项目信息-总数/已完成/未完成
     */
    public Main selectProject(){
        /**
         * 返回的数据
         */
        Main result=new Main();
        result.setAllCnt(mainDao.selectProject(null).getAllCnt());
        result.setOkCnt(mainDao.selectOkProject(null).getOkCnt());
        result.setUnCnt(mainDao.selectUnProject(null).getUnCnt());
        return result;
    }

    /**
     * 按年 查询项目查询项目信息-完成总数/已完成/未完成
     */
    public Main selectProjectByYear(Integer kind,Integer year){
        /**
         * 条件
         */
        Main con=new Main();
        /**
         * 年份
         */
        if(kind==1){
            con.setKind(1);
            con.setYear(year);
        }
        /**
         * 返回的数据
         */
        Main result=new Main();
        result.setAllCnt(mainDao.selectProject(con).getAllCnt());
        result.setOkCnt(mainDao.selectOkProject(con).getOkCnt());
        result.setUnCnt(mainDao.selectUnProject(con).getUnCnt());
        return result;
    }
    /**
     * 按月 查询项目查询项目信息-完成总数/已完成/未完成
     */
    public Main selectProjectByMonth(Integer kind,String month){
        /**
         * 条件
         */
        Main con=new Main();
        /**
         * 年份
         */
        if(kind==2){
            con.setKind(2);
            con.setMonth(month);
        }
        /**
         * 返回的数据
         */
        Main result=new Main();
        result.setAllCnt(mainDao.selectProject(con).getAllCnt());
        result.setOkCnt(mainDao.selectOkProject(con).getOkCnt());
        result.setUnCnt(mainDao.selectUnProject(con).getUnCnt());
        return result;
    }

    /**
     * 待创建的商务任务+待审核的商务任务
     *  1预算审核2报价审核3合同审核4款票审核6核算审核 7概算审核
     * @return
     */
    public Main selectUnCreAndAppByKind(){

        Main result=new Main();
        result.setUnAppGaiSuan(mainDao.selectUnAppByKind(7).getAllCnt());
        result.setUnAppYuSuan(mainDao.selectUnAppByKind(1).getAllCnt());
        result.setUnAppHeSuan(mainDao.selectUnAppByKind(6).getAllCnt());
        result.setUnAppBaoJia(mainDao.selectUnAppByKind(2).getAllCnt());
        result.setUnAppHeTong(mainDao.selectUnAppByKind(3).getAllCnt());
        result.setUnAppKuanPiao(mainDao.selectUnAppByKind(4).getAllCnt());

        result.setUnCreGaiSuan(mainDao.selectUnCreByKind(7).getAllCnt());
        result.setUnCreYuSuan(mainDao.selectUnCreByKind(1).getAllCnt());
        result.setUnCreHeSuan(mainDao.selectUnCreByKind(6).getAllCnt());
        result.setUnCreBaoJia(mainDao.selectUnCreByKind(2).getAllCnt());
        result.setUnCreHeTong(mainDao.selectUnCreByKind(3).getAllCnt());
        result.setUnCreKuanPiao(mainDao.selectUnCreByKind(4).getAllCnt());
        return result;

    }
    /**
     * 项目组的任务的完成情况统计
     * @return
     */
    public List<Main> selectGroupCnt(){
        return mainDao.selectGroupCnt();
    }

    /**
     * 待审核数量
     * @return
     */
    public Main unAppInfo(){
        Main m=new Main();
        User user = ShiroUtils.getUser();
        Main con=new Main();
        con.setKind(4);
        con.setUserId(user.getId());
        m.setUnAppKuanPiao(mainDao.unAppInfo(con).getAllCnt());
        con.setKind(3);
        m.setUnAppHeTong(mainDao.unAppInfo(con).getAllCnt());
        con.setKind(2);
        m.setUnAppBaoJia(mainDao.unAppInfo(con).getAllCnt());
        con.setKind(1);
        m.setUnAppYuSuan(mainDao.unAppInfo(con).getAllCnt());
        con.setKind(7);
        m.setUnAppGaiSuan(mainDao.unAppInfo(con).getAllCnt());
        con.setKind(6);
        m.setUnAppHeSuan(mainDao.unAppInfo(con).getAllCnt());
        return m;
    }
    /**
     * 首页柱状图的数据
     * @param model
     * @return
     */
    public List<Main> selectBarData(Main model){



        List<Main> list=mainDao.selectBarData(model);
        return list;
    }

    /**
     * 获取当前时间所在周的周一和周日的日期时间
     * @return
     */
    public static Map<String,String> getWeekDate() {
        Map<String,String> map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Date mondayDate = cal.getTime();
        String weekBegin = sdf.format(mondayDate);

        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        String weekEnd = sdf.format(sundayDate);

        map.put("mondayDate", weekBegin);
        map.put("sundayDate", weekEnd);
        return map;
    }

    /**
     *
     * @param productMixModel
     * @return
     * @Author: China.kxx
     * @Date: 2021/4/13 12:44
     */
    public List<productMixModel> productSelect(productMixModel productMixModel) {
        return mainDao.selectProduct(productMixModel);
    }
    /**
     * 按项目ID查询项目任务信息
     *
     * @param proId
     * @return
     */
    public productMixModel selectProjectInfo(Integer proId) {
        return mainDao.selectProjectInfo(proId);
    }

    /**
     * 查询产品结构
     *
     * @param proId
     * @return
     */
    public List<DsnProStruct> findCheckProStructByQuery(Integer proId) {
        return mainDao.selectProductDetailList(proId, 0);
    }

}
